# coding=utf-8
import os

import xlrd
import xlwt
from xlutils.copy import copy

class mysheetwrite:
    def open(self,namepath,namesheet):
        self.namepath = namepath
        if os.path.exists(namepath) :
            oldWb = xlrd.open_workbook(namepath)
            self.newWb = copy(oldWb)  # 复制
            try:
                self.book = self.newWb.get_sheet(namesheet)  # 取sheet表
            except :
                self.book = self.newWb.add_sheet(namesheet)
            else:
                pass
        else:
            self.newWb = xlwt.Workbook(encoding='utf-8')
            self.book = self.newWb.add_sheet(namesheet)

    def save(self):
        self.newWb.save(self.namepath)
    def saveto(self,newnamepath):
        self.newWb.save(newnamepath)

    def setdata(self,row, col:int,data:str):
        self.book.write(row,col,data)

    def setdatac(self, row, strcol:str,data:str):
        strcol = strcol.upper()
        return self.setdata(row, ord(strcol[0]) - ord('A'),data)


if __name__ == '__main__':
    book = mysheetwrite()
    book.open("test.xlsx","test")
    book.setdata(1,1,"testfunc")
    book.saveto("testcode.xlsx")